package com.crm.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.crm.commons.exception.BusinessException;
import com.crm.entity.CrmActivity;
import com.crm.entity.CrmClue;
import com.crm.mapper.CrmClueActivityRelationMapper;
import com.crm.mapper.CrmClueMapper;
import com.crm.mapper.CrmClueRemarkMapper;
import com.crm.mapper.CrmDataValueMapper;
import com.crm.service.ICrmClueService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.crm.service.ICrmUserService;
import com.crm.vo.*;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

import static com.crm.commons.exception.enumVo.BussinessExceptionEnum.CLUE_NOT_EXISTS;
import static com.crm.commons.exception.enumVo.BussinessExceptionEnum.PARAM_VALID;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author junxian, dou
 * @since 2024-11-25
 */
@Service
public class CrmClueServiceImpl extends ServiceImpl<CrmClueMapper, CrmClue> implements ICrmClueService {


    @Resource
    private ICrmUserService crmUserService;

    @Resource
    private CrmDataValueMapper crmDataValueMapper;

    @Resource
    private CrmClueRemarkMapper crmClueRemarkMapper;

    @Resource
    private CrmClueActivityRelationMapper clueActivityRelationMapper;

    @Override
    public void saveCreateClue(Clue clue) {
        if (!ObjectUtil.isAllNotEmpty(clue)) {
            throw new BusinessException(PARAM_VALID);
        }
        CrmClue crmClue = clue.createCrmClue();

        save(crmClue);
    }

    @Override
    public Map<String, List> initClueOptions() {
        HashMap<String, List> stringListHashMap = new HashMap<>();
        stringListHashMap.put("userList", crmUserService.getOwnerList());
        stringListHashMap.put("appellationList", crmDataValueMapper.queryDicValueByTypeCode("appellation"));
        stringListHashMap.put("clueStateList", crmDataValueMapper.queryDicValueByTypeCode("clueState"));
        stringListHashMap.put("sourceList", crmDataValueMapper.queryDicValueByTypeCode("source"));
        return stringListHashMap;
    }

    @Override
    public PageBean<CrmClue> pageList(PageBean pageBean) {
        Map<String, String> map = pageBean.getQuery();
        String name = map.get("name");
        String company = map.get("company");
        String mphone = map.get("mphone");
        String source = map.get("source");
        String owner = map.get("owner");
        String phone = map.get("phone");
        String state = map.get("state");

        int pageSize = pageBean.getPageSize();
        int pageNum = pageBean.getPageNum();

        Page<CrmClue> crmCluePage = new Page<>(pageNum, pageSize);

        IPage<CrmClue> iPage = this.baseMapper.pageList(crmCluePage, name, company, mphone, source, state, owner, phone);

        return new PageBean<CrmClue>(iPage.getTotal(), iPage.getRecords());
    }

    @Override
    public CrmClue getClueInfo(String id) {

        if (ObjectUtil.isEmpty(id)) {
            throw new BusinessException(PARAM_VALID);
        }
        CrmClue crmClue = getById(id);
        return crmClue;
    }

    @Override
    public void updateClue(Clue clue) {
        if (ObjectUtil.isEmpty(clue)) {
            throw new BusinessException(PARAM_VALID);
        }
        CrmClue crmClue = clue.createCrmClue();

        updateById(crmClue);
    }

    @Override
    public ClueDetail getClueDeatil(String id) {
        Clue clueInfo = getClueinfo(id);
        if (ObjectUtil.isEmpty(clueInfo)) throw new BusinessException(CLUE_NOT_EXISTS);
        List<ClueRemark> crmClueRemarks = crmClueRemarkMapper.getClueRemarkListByClueId(clueInfo.getId());
        ClueDetail clueDetail = new ClueDetail( crmClueRemarks, clueInfo);
        return clueDetail;
    }

    @Override
    public void deleteClue(String[] id) {
        if (id.length > 0) {
            removeBatchByIds(Arrays.asList(id));
        }

    }

    @Override
    public Clue getClueinfo(String id) {
        if (ObjectUtil.isEmpty(id)) throw new BusinessException(PARAM_VALID);
        Clue clueInfo = this.baseMapper.getClueDeatil(id);
        return clueInfo;
    }
}
